<template>
	<view class="view">
		<view class="head">
			<image src="https://yixianfeng.oss-cn-beijing.aliyuncs.com/static/images/img_integral.png" mode="widthFix">
			</image>
		</view>
		<view class="menu">
			<view class="item" @click="to('/pages/other/integral/integral')">
				<image src="/static/images/ic_integral.png" mode="aspectFill"></image>
				<view>积分：<text>{{account.accountBalance || 0}}</text></view>
			</view>
			<view class="divider"></view>
			<view class="item" @click="to('/pages/other/integral/record')">
				<image src="/static/images/ic_exchange.png" mode="aspectFill"></image>
				<view>兑换记录</view>
			</view>
		</view>
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="index"
				@click="to('/pages/other/integral/product?id=' + item.id)">
				<image :src="item.productPic || '/static/images/ic_img.png'"></image>
				<view class="name">{{item.productName}}</view>
				<view class="bottom">
					<view>{{item.price}}积分</view>
					<view>剩余：{{item.stock}}件</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				account: {},
				finished: false,
				pageNo: 1,
				list: [],
			};
		},
		onLoad() {
			this.init();
		},
		methods: {
			init() {
				this.getAccountInfo();
				this.pageNo = 1;
				this.list = [];
				this.getList();
			},
			getAccountInfo() {
				this.$api.http('/user/userAccountInfo/getAccountInfoByUserId', 'GET', {
					userId: uni.getStorageSync('userInfo').id
				}, res => {
					if (res.data.code == 200) {
						this.account = res.data.result;
					} else {
						this.$Toast(res.data.message || '请求失败，请重试！');
					}
				});
			},
			getList() {
				if (this.finished) {
					return;
				}
				this.$api.http('/product/pmsIntegralProduct/api/n/list', 'GET', {
					pageNo: this.pageNo++,
					pageSize: 10,
				}, res => {
					if (res.data.code == 200) {
						if (res.data.result.records.length == 0) {
							this.finished = true;
						}
						this.list = this.list.concat(res.data.result.records);
					} else {
						this.$Toast(res.data.message || '请求失败，请重试！');
					}
					uni.stopPullDownRefresh();
				})
			},
			to(url) {
				uni.navigateTo({
					url
				});
			},
		},
		onReachBottom() {
			this.getList();
		},
		onPullDownRefresh() {
			this.init();
		},
	}
</script>

<style lang="less">
	.view {
		width: 100%;
		background-color: #f1efef;

		.head {
			image {
				width: 100%;
			}
		}

		.menu {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			margin-left: 20rpx;
			margin-top: -80rpx;
			margin-right: 20rpx;
			background-color: #FFFFFF;
			border-radius: 40rpx;
			z-index: 2;

			.item {
				flex: 1;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				height: 80rpx;
				font-size: 28rpx;

				image {
					width: 45rpx;
					height: 45rpx;
					margin-right: 10rpx;
				}

				text {
					color: #E5AE54;
				}
			}

			.divider {
				width: 2rpx;
				height: 30rpx;
				background-color: #F1F1F1;
			}
		}

		.list {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			padding: 10rpx;

			.item {
				margin: 10rpx;
				width: 345rpx;
				background-color: #FFFFFF;
				border-radius: 10rpx;

				image {
					width: 100%;
					height: 345rpx;
					border-radius: 10rpx 10rpx 0 0;
				}

				.name {
					margin: 20rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					font-size: 30rpx;
					color: #333333;
				}

				.bottom {
					margin: 10rpx 20rpx 30rpx 20rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: space-between;

					view {
						font-size: 24rpx;
						color: #999999;

						&:first-child {
							font-size: 28rpx;
							color: #E5AE54;
						}
					}
				}
			}
		}
	}
</style>